﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public class PeaceCommand : Command, ICommandInfo
    {
        public override string HelpText { get { return "Stops all attacks"; } }

        public static List<string> Words = new List<string>() { "PEACE" };

        public override List<string> FirstWords { get { return Words; } }

        public override CommandToPerform CreateCommand(IGameThing source, string first, string input)
        {
            CommandToPerform cmd = CommandFactory.CreatePeace(source.AssociatedID);
            cmd.Arguments = new PropertyData(new IdentifierAndValue("typed", first), new IdentifierAndValue("second", input));
            return cmd;
        }

        public override bool CheckIsValid(IGameThing source, string first, string input)
        {
            return true;
        }

        [MethodVisible]
        public static EventToPerceive CreatePeaceObservableEvent(GameChar source, GameChar target)
        {
            return new GeneralEventToPerceive()
            {
                ShowToSource = new Event("You stop attacking.", ViewType.See),
                ShowToObservers = new Event("~source stops attacking.", ViewType.See),
                Sources = new AssociatedIDCollection(source.AssociatedID)
            };
        }

        [MethodVisible]
        public static List<PerceivedEventAndAudience> Peace(CommandToPerform cmd, IGameThing src, IGameThing tar, PropertyData args, PropertyData modifyingArguments)
        {
            GameChar source = src as GameChar;
            GameChar target = tar as GameChar;
            List<PerceivedEventAndAudience> events = new List<PerceivedEventAndAudience>();
            string typed = args.GetArgument<string>("typed");
            string second = args.GetArgument<string>("second");

            if (!string.IsNullOrEmpty(second) && second.Equals("STOP", StringComparison.OrdinalIgnoreCase))
            {
                CombatHandler.StopAttacking(source, true);
            }
            else
            {
                CombatHandler.StopAttacking(source, false);
            }

            PerceivedEventAndAudience p = new PerceivedEventAndAudience()
            {
                Event = PeaceCommand.CreatePeaceObservableEvent(source, target),
                Audience = source.GameLocation.Things
            };
            events.Add(p);

            return events;
        }
    }

    public static partial class CommandFactory
    {        
        [MethodVisible]
        public static CommandToPerform CreatePeace(long source)
        {
            CommandToPerform cmd = new CommandToPerform()
            {
                ApplyToID = source,
                PerformMethod = new Method("PeaceCommand", "Peace")
            };
            return cmd;
        }
    }
}